package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DictionaryItem;
import cn.wolfcode.crm.service.IDictionaryItemService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionaryItem")
public class DictionaryItemController {
    @Autowired
    public IDictionaryItemService dictionaryItemService;

    @RequestMapping("selectAll")
    @ResponseBody
    public List<DictionaryItem> selectAll() {
        return
            dictionaryItemService.selectAll();
    }

    @RequestMapping("getItemByDictionaryId")
    @ResponseBody
    public List<DictionaryItem> getItemByDictionaryId(String dictionaryId) {
        List<DictionaryItem> itemByDictionaryId = dictionaryItemService.getItemByDictionaryId(dictionaryId);
        return itemByDictionaryId;
    }

    @RequestMapping("getItemBySn")
    @ResponseBody
    public List<DictionaryItem> getItemBySn(String sn) {
        return dictionaryItemService.getItemBySn(sn);
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"dictionaryItem:delete","字典明细删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            dictionaryItemService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"dictionaryItem:saveOrUpdate","字典明细新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(DictionaryItem dictionaryItem){
        try {
            if(dictionaryItem.getId() != null){
                dictionaryItemService.updateByPrimaryKey(dictionaryItem);
            }else{
                dictionaryItemService.insert(dictionaryItem);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
}
